


import * as echarts from 'echarts'
 export default {
   methods: {
      TreeCharts_V2Init(v){
        const treeData = [
          {
            name: 'root',
            children: [
              {
                name: 'Child A',
                children: [
                  {
                    name: 'Leaf C',
                  },
                  {
                    name: 'Leaf D',
                  },
                  {
                    name: 'Leaf E',
                  },
                  {
                    name: 'Leaf F',
                  },
                ],
              },
              {
                name: 'Child B',
                children: [
                  {
                    name: 'Leaf G',
                  },
                  {
                    name: 'Leaf H',
                  },
                ],
              },
              {
                name: 'Child D',
              },
              {
                name: 'Child F',
                children: [
                  {
                    name: 'Leaf J',
                  },
                  {
                    name: 'Leaf K',
                  },
                ],
              },
            ],
          },
        ]

        let echartEle=  this.$refs[v];
        let myChart = echarts.init(echartEle);

        const {
          dataSourceType,options,staticData
        } = this.controlProperties[v]

        if (dataSourceType === 'static' && Object.keys(staticData).length) {
          options.series = options.series.map((item, index) => {
            return { ...item, data: staticData.series[index].data, ...item.basicConfig }
          })
        } else {
          options.series = options.series.map((item, index) => {
            return { ...item, data: treeData, ...item.basicConfig }
          })
        }
        options && myChart.setOption(options);
      },
      TreeCharts_V2SetOption(v){
        const { options, flowResult, dynamicData: { name=[], children=[] } } = this.controlProperties[v]
        let { series } = options
        // series
        series = series.map((item) => {
          item.data = this.recur(flowResult,name[name.length-1].dataKey,children[children.length-1].dataKey)
        })
        let echartEle = this.$refs[v];
        let myChart = echarts.init(echartEle);
        options && myChart.setOption(options);
      },
      // 递归替换属性名
      recur(data,name, children) {
        return data.map(item=>{
            return {name:item[name],children: this.recur(item[children]||[],name,children)}
        })
      }
   }
  }
